# -*- coding:utf-8 -*-
'''
Created on 02.05.2010

@author: yserdyukov
'''

from django import template
from django.utils.encoding import force_unicode
from mimetypes import guess_type
import re
register = template.Library()

@register.filter
def truncatewords_length(value, length):
    """
    Truncates a string by words after a certain number of chars
    Argument: Number of chars to truncate after.
    """
    length = int(length)
    suffix = u'…'
    try:
        if len(value) <= length:
            return value
        else:
            return ' '.join(value[:length + 1].split(' ')[0:-1]) + suffix
    except:
        return value